package com.example.dense_rack_server.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dense_rack_server.entity.ArchivesType;
import com.example.dense_rack_server.service.ArchivesTypeService;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案分类 前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-03
 */
@Controller
@RequestMapping("/archives/type")
public class ArchivesTypeController {

    @Resource
    public ArchivesTypeService archivesTypeService;

    /**
     * 档案分类页面
     */
    @GetMapping("/view")
    public String preview() {
        return "archivesType/table-list";
    }

    /**
     * 绘制分类树
     */
    @GetMapping("/buildTypeTree")
    @ResponseBody
    public Map<String, Object> buildTypeTree() {
        return archivesTypeService.buildTypeTree();
    }

    /**
     * 档案分类列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> typeList(@RequestParam(value = "typeName", required = false) String typeName) {
        return archivesTypeService.typeList();
    }

    /**
     * 档案分类新增页面
     */
    @GetMapping("/add")
    public String add() {
        return "archivesType/add";
    }

    /**
     * 绘制分类树
     */
    @GetMapping("/buildTypeTree/table")
    @ResponseBody
    public ResponseModel buildTypeTreeTable() {
        return archivesTypeService.buildTypeTreeTable();
    }

    /**
     * 档案分类新增保存
     */
    @PostMapping("/save")
    @ResponseBody
    public ResponseModel saveType(@RequestBody JSONObject data) {
        return archivesTypeService.saveType(data);
    }


    /**
     * 档案分类修改页面
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView("archivesType/edit");
        modelAndView.addObject("archivesType", archivesTypeService.getById(id));
        return modelAndView;
    }

    /**
     * 档案分类修改保存
     */
    @PostMapping("/update")
    @ResponseBody
    public ResponseModel updateType(@RequestBody JSONObject data) {
        return archivesTypeService.updateType(data);
    }

    /**
     * 档案区域状态修改
     */
    @PutMapping("/change/status")
    @ResponseBody
    public ResponseModel updateTypeStatus(@RequestBody JSONObject data) {
        return archivesTypeService.updateTypeStatus(data);
    }

    /**
     * 判断下面是否还有子节点
     */
    @GetMapping("/has/child/{id}")
    @ResponseBody
    public ResponseModel hasChild(@PathVariable("id") String id) {
        String[] ids = id.split(",");
        for (String areaId : ids) {
            List<ArchivesType> deptList = archivesTypeService.list(new QueryWrapper<ArchivesType>()
                    .eq("parent_id", Convert.toInt(areaId)));
            if (!deptList.isEmpty()) {
                return ResponseModel.fail("此分类下有下级分类,不允许删除");
            }
        }
        return ResponseModel.success();
    }

    /**
     * 档案分类删除
     */
    @DeleteMapping("/delete/save/{id}")
    @ResponseBody
    public ResponseModel deleteType(@PathVariable("id") String id) {
        return archivesTypeService.deleteType(id);
    }
}
